package com.woniu.inlet.timer;

import com.woniu.outlet.dao.InventoryInoutPoMapper;
import com.woniu.outlet.dao.InventoryPoMapper;
import com.woniu.outlet.dao.WorkerPoMapper;
import com.woniu.outlet.po.*;
import com.woniu.utils.SendEmail;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Configuration
@Slf4j
public class WavehouseScheduling {

    @Autowired
    private InventoryPoMapper inventoryPoMapper;

    @Autowired
    private WorkerPoMapper workerPoMapper;

    @Autowired
    private InventoryInoutPoMapper inventoryInoutPoMapper;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private SendEmail sendEmail;

    @Scheduled(fixedRate = 300000)
    public  void sendMessage(){
        RLock rLock = redissonClient.getLock("check");  //获取一把锁对象，此时还没有加锁
        rLock.lock(300000L, TimeUnit.MILLISECONDS);  //加锁，其实就是设置一个key-value ,setnx(“hello”)   默认加的锁都是30s

        InventoryPoExample inventoryPoExample = new InventoryPoExample();
        InventoryPoExample.Criteria criteria = inventoryPoExample.createCriteria();
        criteria.andTypeEqualTo("M");
        List<InventoryPo> inventoryPos = inventoryPoMapper.selectByExample(inventoryPoExample);

        if(inventoryPos.size()==0){
            return;
        }

        List<WorkerPo> workerPos = workerPoMapper.getPruchaseWorker();

        String minMsg = "仓库短缺原料:/n";
        String maxMsg = "仓库爆仓原料:/n";
        int min=0;
        int max=0;
        for (InventoryPo inventoryPo : inventoryPos) {
            if(inventoryPo.getNum()<inventoryPo.getMinNum()){
                min++;
                minMsg=minMsg+"ID:"+inventoryPo.getId()+"-原料编号:"+inventoryPo.getCode()+"-原料名:"
                +inventoryPo.getName()+"/n";
            }

            if(inventoryPo.getNum()>inventoryPo.getMaxNum()){
                max++;
                maxMsg=maxMsg+"ID:"+inventoryPo.getId()+"-原料编号:"+inventoryPo.getCode()+"-原料名:"
                        +inventoryPo.getName()+"/n";
            }
        }

        if(min>0){
            minMsg=minMsg+"请及时采购!";
            sendEmail.sendMsg("物料报单:",minMsg,workerPos.get(0).getEmail());
            min=0;
            minMsg="仓库短缺原料:/n";
        }

        if(max>0){
            maxMsg=maxMsg+"请及时处理!";
            sendEmail.sendMsg("物料报单:",maxMsg,workerPos.get(0).getEmail());
            max=0;
            maxMsg="仓库爆仓原料:/n";
        }


        InventoryInoutPoExample inventoryInoutPoExample = new InventoryInoutPoExample();
        InventoryInoutPoExample.Criteria criteria1 = inventoryInoutPoExample.createCriteria();
        criteria1.andNuonStandardNumGreaterThan(0);
        criteria1.andIsoutEqualTo("S");
        criteria1.andTypeEqualTo("M");
        List<InventoryInoutPo> inventoryInoutPos = inventoryInoutPoMapper.selectByExample(inventoryInoutPoExample);
        if(inventoryInoutPos.size()>0){
            String outMsg="因不达标尚未入库原料有:";
            for (InventoryInoutPo inventoryInoutPo : inventoryInoutPos) {
                outMsg=outMsg+inventoryInoutPo.getId()+"-原料编号:"+inventoryInoutPo.getCode()+"-原料名:"
                        +inventoryInoutPo.getName()+"/n";
            }
            sendEmail.sendMsg("物料报单:",outMsg,workerPos.get(0).getEmail());
        }
    }
}
